package com.handydata.antifraud.rule.funcs;

import java.util.Optional;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.handydata.antifraud.annotation.LockAction;
import com.handydata.antifraud.to.RiskInputTO;
import com.handydata.antifraud.to.RiskMiddleTO;
import com.handydata.common.util.RedisKey;
import com.handydata.common.util.RedisUtil;

/**
 * 统计客户数
 */
@Slf4j
@Component
public class CalcCustomers extends CalculateAdapter {
    @Autowired
    private RedisUtil redisUtil;

//    private static final String CUST_KEY = AlarmInfoCode.REDIS_BANK_COUNT_KEY.getCode();
    @LockAction("calcCustomers")
    @Override
    public void doCalc(RiskInputTO riskInputTO, RiskMiddleTO riskMiddleTO) {
        String idCard = riskInputTO.getIdCard();
        riskMiddleTO.setCalARelR001Cnt(count("nterDeviceIdCode", riskInputTO.getNterDeviceIdCode(), "idCard", idCard));
        riskMiddleTO.setCalARelR002Cnt(count("deviceNic", riskInputTO.getDeviceNic(), "idCard", idCard));
        riskMiddleTO.setCalARelR003Cnt(count("nterDeviceIdentifyCode", riskInputTO.getNterDeviceIdentifyCode(), "nterDeviceIdCode", riskInputTO.getNterDeviceIdCode()));
        riskMiddleTO.setCalARelR004Cnt(count("nterDeviceIdCode", riskInputTO.getNterDeviceIdCode(), "nterDeviceIdentifyCode", riskInputTO.getNterDeviceIdentifyCode()));
        riskMiddleTO.setCalARelR005Cnt(count("deviceIdCode", riskInputTO.getDeviceIdCode(), "idCard", idCard));
        riskMiddleTO.setCalARelR006Cnt(riskMiddleTO.getCalARelR001Cnt());
        riskMiddleTO.setCalARelR007Cnt(riskMiddleTO.getCalARelR002Cnt());
        riskMiddleTO.setCalARelR008Cnt(riskMiddleTO.getCalARelR003Cnt());
        riskMiddleTO.setCalARelR009Cnt(riskMiddleTO.getCalARelR004Cnt());
        riskMiddleTO.setCalARelR010Cnt(riskMiddleTO.getCalARelR005Cnt());

    }


    private int count(String key1, String value, String drKey, String drValue) {

        if(value==null||value.length()<=0){
            return 0;
        }
        if(value.replaceAll("0","").length()<=0){//全是0
            return 0;
        }
        if(drValue==null||drValue.length()<=0){
            drValue="";
        }
		log.info(" run CalcCustomers key1{} value{} drkey{} drValue{} ", key1, value, drKey, drValue);
        Optional<Object> sameValueIdCardCount = Optional.ofNullable(redisUtil.get(RedisKey.REDIS_BANK_COUNT_KEY + key1 + "_" + value + "_" + drKey + "_" + drValue));

        int res = 1;
		if (!sameValueIdCardCount.isPresent()) {
			redisUtil.set(RedisKey.REDIS_BANK_COUNT_KEY  + key1 + "_" + value + "_" + drKey + "_" + drValue, 1, 15552000);
			// return Integer.valueOf(String.valueOf(redisUtil.incr(CUST_KEY + "_" + value +"_"+drKey, 1);
			res = Integer.parseInt(String.valueOf(redisUtil.get(RedisKey.REDIS_BANK_COUNT_KEY  + key1 + "_" + value + "_" + drKey + "_" + drValue))); 
		} else {
			long expiryDate = redisUtil.getExpire(RedisKey.REDIS_BANK_COUNT_KEY  + key1 + "_" + value + "_" + drKey + "_" + drValue);
			if (expiryDate < 0) {
				expiryDate = 15552000;
			}
			redisUtil.incr(RedisKey.REDIS_BANK_COUNT_KEY  + key1 + "_" + value + "_" + drKey + "_" + drValue, 1);
			redisUtil.expire(RedisKey.REDIS_BANK_COUNT_KEY  + key1 + "_" + value + "_" + drKey + "_" + drValue, expiryDate);
			Object obj = redisUtil.get(RedisKey.REDIS_BANK_COUNT_KEY  + key1 + "_" + value + "_" + drKey + "_" + drValue);
			if (obj != null)
				res = Integer.valueOf(obj.toString());
		}
        log.info(" finish CalcCustomers key1{} value{} drkey{} drValue{} result{} ",key1,value,drKey,drValue,res);

        return res;
    }


}
